package com.example.service;

import com.example.entity.BaseCoreEntity;
import com.example.entity.BaseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chentao
 * @create 2023/3/11 17:43
 * @since 1.0.0
 */
@Service
public class BaseDispatcherServiceImpl<T extends BaseEntity> implements BaseDispatcherService<T> {

    @Resource
    private List<BaseCoreService> coreServiceList;

    @Resource
    private ObjectHandlerService<T> objectHandlerService;

    @Override
    public void add(T entity) {
        for (BaseCoreService baseCoreService : coreServiceList) {
            if (entity instanceof BaseCoreEntity) {
                BaseCoreEntity baseCoreEntity = (BaseCoreEntity) entity;
                baseCoreEntity.setModelId(entity.getId());
                baseCoreEntity.setModelName(entity.getClass().getName());
                baseCoreService.add(baseCoreEntity);
            }
        }
        this.objectHandlerService.handlerAdd(entity);
    }

    @Override
    public void delete(T entity) {
        for (BaseCoreService baseCoreService : coreServiceList) {
            baseCoreService.delete(entity);
        }
        this.objectHandlerService.handlerDelete(entity);
    }

    @Override
    public void get(T entity) {
        for (BaseCoreService baseCoreService : coreServiceList) {
            baseCoreService.get(entity);
        }
    }

}